#include "BossStage1.h"
#include "../DeadlyShadowsLastHope/Player.h"
#include "../GameplayStates/GameplayState.h"


#define SHOT_DELAY 10.0f
#define MOVEMENT_DELAY 2.5f

CBossStage1::CBossStage1(void)
{

}

CBossStage1::~CBossStage1(void)
{	
	CDestroyEntityMsg* pMsg = new CDestroyEntityMsg(this);
	CMessageSystem::GetInstance()->SendMsg(pMsg);
	pMsg = nullptr;

}

//Inherited from CEntity///////////////////////////////
void CBossStage1::Update (float fElapsedTime)
{
	if( 0>=GetHealth())
	{
		m_pES->SendEvent("BDeath",CGameplayState::GetInstance());
		CDestroyEntityMsg* pMsg = new CDestroyEntityMsg(this);
		CMessageSystem::GetInstance()->SendMsg(pMsg);
		pMsg = nullptr;
	}


	CPlayer* pPlayer = dynamic_cast<CPlayer*>(this->m_pTarget);
	float distances = (this->GetPosX()-pPlayer->GetPosX());
	if(distances<=1000)
	{
		m_fTimer += fElapsedTime;
		if(m_fTimer>=SHOT_DELAY)
		{
			this->m_cAniInfo.SetName("EBoss1Summon");
			this->m_cAniInfo.SetFrame(0);
			this->m_cAniInfo.SetTime(0);

			this->m_pES->SendEvent("Summon",this);
			m_fTimer=0;
		}
	}
	CEntity::Update(fElapsedTime);
	m_pAM->Update(fElapsedTime,this->m_cAniInfo);
	//Get current animations
	if(this->GetPosY() + this->GetHeight() < CGame::GetInstance()->GetHeight() && this->OnPlatform() == false)
	{
		this->SetVelY(this->GetVelY()+5);
	}
	if(this->GetPosY() + this->GetHeight() > (int)CGame::GetInstance()->GetHeight())
	{
		this->SetPosY((float)CGame::GetInstance()->GetHeight()-this->GetHeight());
	}
	if(this->GetPosX() <= 0)
		if(this->GetVelX() < 0)
		{
			this->SetVelX(0);
			this->SetPosX(0);
		}
		if(this->GetPosX() + this->GetWidth() >= CGame::GetInstance()->GetWidth())
			if(this->GetVelX() > 0)
			{
				this->SetVelX(0);
				this->SetPosX((float)CGame::GetInstance()->GetWidth()-this->GetWidth());
			}

			// CHANGE UP BASED ON CLone level.
			if(this->GetPosY() + this->GetHeight() < CGame::GetInstance()->GetHeight() && this->OnPlatform() == false)
				this->SetVelY(this->GetVelY()+1);
			if(this->GetPosY() + this->GetHeight() > (int)CGame::GetInstance()->GetHeight())
				this->SetPosY((float)CGame::GetInstance()->GetHeight()-this->GetHeight());
			if(this->GetPosX() <= 0)
				if(this->GetVelX() < 0)
				{
					this->SetVelX(0);
					this->SetPosX(0);
				}
				//	if(this->GetPosX() + this->GetWidth() >= CGame::GetInstance()->GetWidth())
				//		if(this->GetVelX() > 0)
				//		{
				//			this->SetVelX(0);
				//			this->SetPosX((float)CGame::GetInstance()->GetWidth()-this->GetWidth());
				//		}
				if(this->m_pTarget->GetPosX() + m_pTarget->GetWidth() < this->GetPosX())
					this->SetVelX(200);
				else if(this->m_pTarget->GetPosX() > this->GetPosX() + this->GetWidth())
					this->SetVelX(-200);

				if(m_battack==false)
					m_fShootTimer+=fElapsedTime;
				if(m_fShootTimer>=MOVEMENT_DELAY&&m_battack==false)
				{
					m_fShootTimer=0;
					m_battack=true;
				}


}

void CBossStage1::Render (void)
{
	this->m_pD3D->GetSprite()->Flush();
	RECT playerRect = {this->GetRect().left - CGameplayState::GetInstance()->GetCamera().camPosX, this->GetRect().top,
		this->GetRect().right - CGameplayState::GetInstance()->GetCamera().camPosX, this->GetRect().bottom};
	m_pD3D->DrawRect(playerRect, 50, 100, 255);

	if(this->m_nCloneLevel==0)
		m_pAM->Render(m_cAniInfo,playerRect.left-42,playerRect.top-25,1,this->GetFacing());
	//	else if (this->m_nCloneLevel==1)
	//m_pAM->Render(m_cAniInfo,playerRect.left-15,playerRect.top+3,0.75,this->GetFacing());
	//	else if (this->m_nCloneLevel==2)
	//m_pAM->Render(m_cAniInfo,playerRect.left-15,playerRect.top+3,0.5,this->GetFacing());
}

RECT CBossStage1::GetRect(void) const
{
	RECT rSelf = {(LONG)GetPosX(),	(LONG)GetPosY(),	(LONG)(GetPosX() + GetWidth()),	(LONG)(GetPosY() + GetHeight())	};
	return rSelf;
}

bool CBossStage1::CheckCollision( IEntity* pOther )
{

	if(CEntity::CheckCollision(pOther) == false)
		return false;
	RECT me = this->GetRect();
	RECT other = pOther->GetRect();
	RECT temp;
	IntersectRect(&temp, &me, &other);
	switch(pOther->GetType())
	{
	case OBJ_TILE:
		{
			if(this->GetPosY() <= other.bottom)
				this->SetVelY(this->GetVelY() + 5);
			if(this->GetPosY() >= other.top-3 )
			{
				if (temp.left !=0)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.left-this->GetWidth());
				}

				if(/*temp.right !=0*/ me.left > other.left && me.left < other.right)
				{
					this->SetVelX(0);
					this->SetPosX((float)other.right);
				}
			}
			if(this->GetVelY() > 0)
			{
				if(temp.bottom !=0)
				{
					this->OnPlatform(true);

					if(this->GetVelY() > 0)
						this->SetVelY(0); 
				}
			}
		}
		break;
	case OBJ_PLAYER:
		{
			if(this->GetPosY() <= other.bottom)
				this->SetVelY(this->GetVelY() + 5);
			if(this->GetPosY() >= other.top-3 )
			{
				if (temp.left !=0)
				{
					if(m_battack==true)
					{
						m_pES->SendEvent("BAttacked",CGameplayState::GetInstance());
						m_cAniInfo.SetName("EBoss1Attack");	
						m_cAniInfo.SetNext("EBoss1Walk");
						m_cAniInfo.SetTime(0);
						m_cAniInfo.SetFrame(0);
					}
					/*
					SOund/damage events
					*/ 
				}
				if(/*temp.right !=0*/ me.left > other.left && me.left < other.right)
				{
					if(m_battack==true)
					{
						m_pES->SendEvent("BAttacked",CGameplayState::GetInstance());
						m_cAniInfo.SetName("EBoss1Attack");	
						m_cAniInfo.SetNext("EBoss1Walk");
						m_cAniInfo.SetTime(0);
						m_cAniInfo.SetFrame(0);
					}
				}
			}
		}
		break;
	}

	return true;
}
///////////////////////////////////////////////////////

void CBossStage1::HandleEvent(CNEvent* pEvent)
{
	if(pEvent->GetEventID() == "Summon")
	{
		m_pES->SendEvent("BSummon",CGameplayState::GetInstance());
		if(this->m_nCloneLevel==0)
			m_pSS->Spawn(this->m_pTarget,"Base",2,(int)this->GetPosX()+10,(int)this->GetPosY(),2);
		//if(this->m_nCloneLevel==1)
		//	m_pSS->Spawn(this->m_pTarget,"Boss1",3,(int)this->GetPosX()+10,(int)this->GetPosY(),2);
		//if(this->m_nCloneLevel==2)
		//	m_pSS->Spawn(this->m_pTarget,"Boss1",4,(int)this->GetPosX()+10,(int)this->GetPosY(),2);
	}
	//	if(pEvent->GetEventID() == "Clone")
	//	{
	//		if(this->m_nCloneLevel==0)
	//			m_pSS->Spawn(this->m_pTarget,"Boss1",2,(int)this->GetPosX(),(int)this->GetPosY(),1);
	//		if(this->m_nCloneLevel==1)
	//			m_pSS->Spawn(this->m_pTarget,"Boss1",1,(int)this->GetPosX(),(int)this->GetPosY(),1);
	//	}

	if(pEvent->GetEventID() == "take damage")
	{
		this->m_nHealth -= 75;
		SetHealth(m_nHealth);
		CPlayer* pPlayer = dynamic_cast<CPlayer*>(this->m_pTarget);
		pPlayer->SetCurrentHits(pPlayer->GetCurrentHits()+1);
		this->SetVelX(-(this->GetVelX()+10));
	}
	if(pEvent->GetEventID() == "take shield damage")
	{
		this->m_nHealth -=100;
		SetHealth(m_nHealth);
		CPlayer* pPlayer = dynamic_cast<CPlayer*>(this->m_pTarget);
		pPlayer->SetCurrentHits(pPlayer->GetCurrentHits()+1);
		this->SetVelX(-(this->GetVelX()+10));
	}
	if(pEvent->GetEventID() == "Return")
	{

		m_cAniInfo.SetName("EBoss1Walk");					//add this
		m_cAniInfo.SetNext("EBoss1Attack");					//add this
		m_cAniInfo.SetTime(0);
		m_cAniInfo.SetFrame(0);
		m_battack=false;
	}
	if(pEvent->GetEventID() == "Rest")
	{
		m_cAniInfo.SetName("EBoss1Walk");					//add this
		m_cAniInfo.SetNext("EBoss1Attack");					//add this
		m_cAniInfo.SetTime(0);
		m_cAniInfo.SetFrame(0);
		m_battack=false;
	}
}

bool CBossStage1::Init(CEntity* pTarget,int x,int y,int speedset)
{
	if(pTarget->GetType() != OBJ_PLAYER)
		return false;
	else
	{
		m_pES = CEventSystem::GetInstance();
		m_pTM	= CSGD_TextureManager::GetInstance();
		m_pXA	= CSGD_XAudio2::GetInstance();
		m_pD3D	= CSGD_Direct3D::GetInstance();
		m_pDI	= CSGD_DirectInput::GetInstance();
		m_pMS	= CMessageSystem::GetInstance();
		m_pAM	= CAnimationManager::GetInstance();
		m_pSS	= CSpawnSystem::GetInstance();

		CEventSystem::GetInstance()->RegisterClient("Summon", this); 
		CEventSystem::GetInstance()->RegisterClient("Clone", this);
		CEventSystem::GetInstance()->RegisterClient("take damage", this);
		CEventSystem::GetInstance()->RegisterClient("take shield damage", this);
		CEventSystem::GetInstance()->RegisterClient("Return", this);
		CEventSystem::GetInstance()->RegisterClient("Rest", this);


		//Set planned animations
		m_cAniInfo.SetName("EBoss1Walk");
		m_cAniInfo.SetNext("EBoss1Summon");
		m_cAniInfo.SetTime(0);
		m_cAniInfo.SetFrame(0);

		this->SetHeight(64);
		this->SetWidth(64);

		m_eType = OBJ_BOSS1;
		this->SetFacing(true);
		this->SetDensity(1);

		this->SetPosX(float(x));
		this->SetPosY(float(y));//(float)CGame::GetInstance()->GetHeight() - this->GetHeight()-500);

		this->SetVelX(0);
		this->SetVelY(0);
		this->m_pTarget = pTarget;
		this->OnPlatform(false);
		m_fTimer =0;
		if(speedset==0)
		{
			this->m_nHealth = 400;
			SetHealth(m_nHealth);
			this->m_fShootTimer =0.75f;
			this->m_nCloneLevel=0;
		}
		else if(speedset==1)
		{
			this->m_nHealth = 200;
			SetHealth(m_nHealth);
			this->m_fShootTimer =0.50f;
			this->m_nCloneLevel=1;
		}
		else if(speedset==2)
		{
			this->m_nHealth = 100;
			SetHealth(m_nHealth);
			this->m_fShootTimer =0.25f;
			this->m_nCloneLevel=2;
		}
	}
	return true;

}